package com.stu.config;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.stu.aq.*;
import com.stu.usb.SerialIOManager;

public class ConfigTransmitterFragment extends CommandFragment implements SerialIOManager.Listener, View.OnTouchListener, NumberEntryPopup.NumberEntryListener {
	private String status;
	private View view;
	private StatusManager statusManager;
	private int rows;
	private String[] values;

	public ConfigTransmitterFragment() {
		status = null;
		view = null;
		statusManager = new StatusManager();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	  Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.config_trans, container, false);
		TableLayout t = (TableLayout)view;
		rows = t.getChildCount();
		values = new String[rows];
		for( int i = 0; i < rows; i++ ) {
			TableRow row = (TableRow)t.getChildAt( i );
			EditText et = (EditText)row.getChildAt( 1 );	// column 2
			et.setOnTouchListener( this );
			et.setTag( i );
		}
		return view;
	}
	@Override
	public void onNewData(final byte[] data) {
		if( status == null )
			status = new String( data );
		else
			status += new String( data );
		// make sure we have the entire status by
		//  looking for a newline at the end
		int ix = status.length();
		if( status.charAt(ix-1) != '\n' )
			return;
		status = status.trim();	// remove newline and trailing spaces
		ix = status.length();
		// don't include trailing comma in substring
		if( status.charAt(ix-1) == ',' )
			ix--;
		final String strings[] = status.substring(0, ix).split( "," );
		if( strings.length != rows ) {
			status = null;
			mListener.OnCommandWarning( getId(), new AQException(ConfigTransmitterFragment.class.getSimpleName() +
			  ": Expected length " + rows + " got " + strings.length) );
			return;
		}
		for( int i = 0; i < rows; i++ )
			values[i] = strings[i];
		status = null;
		statusManager.handleState( new StatusData(this, values) );
	}

	@Override
	public void onRunError(Exception e) {
	}

	@Override
	public void clear() {
		if( view != null ) {
			TableLayout t = (TableLayout)view;
			for( int i = 0; i < rows; i++ ) {
				TableRow row = (TableRow)t.getChildAt( i );
				((EditText)row.getChildAt(1)).setText( "" );
			}
		}
	}

	@Override
	public void update( OnCommandListener l, SerialIOManager m ) {
		status = null;
		mListener = l;
		m.setListener( this );
		String s = "f";
		m.writeAsync( s.getBytes() );
	}

	@Override
	public void updateUI(Object statusData) {
		String[] values = (String[])statusData;
		TableLayout t = (TableLayout)view;
		for( int i = 0; i < rows; i++ ) {
			TableRow row = (TableRow)t.getChildAt( i );
			((EditText)row.getChildAt(1)).setText( values[i] );
		}
		mListener.OnCommandComplete( getId(), FragmentState.UPDATING );
	}

	@Override
	public void stopUpdate(OnCommandListener l, SerialIOManager m) {
	}
	
	public void write( OnCommandListener l, SerialIOManager m ) {
		String s = "F";
		TableLayout t = (TableLayout)view;
		for( int i = 0; i < rows; i++ ) {
			TableRow row = (TableRow)t.getChildAt( i );
			s += ((EditText)row.getChildAt(1)).getText() + ";";
		}
		m.writeAsync( s.getBytes() );
		mListener.OnCommandComplete( getId(), FragmentState.WRITING );
	}

	@Override
	public void OnDone(EditText v) {
		String s = v.getText().toString();
		if( !s.isEmpty() && !s.contains(".") )
			v.setText( s + ".0" );		// add decimal
		v.selectAll();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if( event.getActionMasked() == MotionEvent.ACTION_UP ) {
			((AQ)getActivity()).showEntryDialog( this, (EditText)v, false );
		} else if( event.getActionMasked() == MotionEvent.ACTION_DOWN )
			v.requestFocus();
		return true;
	}
}
